package com.ruoyi.basicData.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basicData.mapper.MaterialManagementMapper;
import com.ruoyi.basicData.domain.MaterialManagement;
import com.ruoyi.basicData.service.IMaterialManagementService;

/**
 * 物料管理Service业务层处理
 * 
 * @author admin
 * @date 2024-06-27
 */
@Service
public class MaterialManagementServiceImpl implements IMaterialManagementService 
{
    @Autowired
    private MaterialManagementMapper materialManagementMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询物料管理
     * 
     * @param materialId 物料管理主键
     * @return 物料管理
     */
    @Override
    public MaterialManagement selectMaterialManagementByMaterialId(Long materialId)
    {
        return materialManagementMapper.selectMaterialManagementByMaterialId(materialId);
    }

    /**
     * 查询物料管理列表
     * 
     * @param materialManagement 物料管理
     * @return 物料管理
     */
    @Override
    public List<MaterialManagement> selectMaterialManagementList(MaterialManagement materialManagement)
    {
        return materialManagementMapper.selectMaterialManagementList(materialManagement);
    }

    /**
     * 新增物料管理
     * 
     * @param materialManagement 物料管理
     * @return 结果
     */
    @Override
    public int insertMaterialManagement(MaterialManagement materialManagement)
    {
        materialManagement.setCreateTime(new Date());
        System.out.println(new Date());
        return materialManagementMapper.insertMaterialManagement(materialManagement);
    }


    /**
     * 批量新增物料管理
     *
     * @param materialManagements 物料管理List
     * @return 结果
     */
    @Override
    public int batchInsertMaterialManagement(List<MaterialManagement> materialManagements)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(materialManagements)) {
            try {
                for (int i = 0; i < materialManagements.size(); i++) {
                    int row = materialManagementMapper.insertMaterialManagement(materialManagements.get(i));
                    // 防止内存溢出，没100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%3 == 0) || i == materialManagements.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }
    /**
     * 修改物料管理
     * 
     * @param materialManagement 物料管理
     * @return 结果
     */
    @Override
    public int updateMaterialManagement(MaterialManagement materialManagement)
    {
        return materialManagementMapper.updateMaterialManagement(materialManagement);
    }

    /**
     * 批量删除物料管理
     * 
     * @param materialIds 需要删除的物料管理主键
     * @return 结果
     */
    @Override
    public int deleteMaterialManagementByMaterialIds(Long[] materialIds)
    {
        return materialManagementMapper.deleteMaterialManagementByMaterialIds(materialIds);
    }

    /**
     * 删除物料管理信息
     * 
     * @param materialId 物料管理主键
     * @return 结果
     */
    @Override
    public int deleteMaterialManagementByMaterialId(Long materialId)
    {
        return materialManagementMapper.deleteMaterialManagementByMaterialId(materialId);
    }

}
